﻿@using BlazorComponent;
@using Masa.Blazor.Presets;
@namespace ThingsGateway.Components
@using Masa.Blazor
@typeparam TItem where TItem : IPrimaryIdEntity, new()
@typeparam SearchItem where SearchItem : IBasePageInput, new()
@typeparam AddItem where AddItem : class, new()
@typeparam EditItem where EditItem : class, new()
@using Microsoft.AspNetCore.Components;
@using ThingsGateway.Core;
@using ThingsGateway.Core.Extension;
@inherits BaseComponentBase

@if (IsShowToolbar)
{
    <MCard Class="ma-2" Outlined Rounded=true Elevation=1 @onkeydown="Enter">

         <MRow Class="py-2 px-2" NoGutters Align="AlignTypes.Start">
             <MForm Model="SearchModel" EnableValidation EnableI18n @ref="SearchForm">
                 <MRow NoGutters Align="AlignTypes.Start">
                     @if (SearchTemplate is not null)
                    {
                        @SearchTemplate(SearchModel)
                    }
                </MRow>
            </MForm>
            @if (IsShowSearchKey)
            {
                <MTextField Dense Style="max-width:200px;" HideDetails=@("auto") Class="mx-2 my-1" Outlined
                             @bind-Value="SearchModel.SearchKey" Label=@SearchModel.Description(x => x.SearchKey) />
            }
            @if (IsShowQueryButton)
            {
                @if (IsShowClearSearch)
                {
                    <MButton Color="info" Class="mx-2 my-1" OnClick=ClearClick>@AppService.I18n.T("清空搜索")</MButton>
                }
                <MButton Color="info" Class="mx-2 my-1" Loading="QueryLoading" OnClick=QueryClickAsync>@AppService.I18n.T("查询")</MButton>
            }
            @if (IsShowAddButton)
            {
                <MButton Color="info" Class="mx-2 my-1" OnClick=@(AddClick)>@AppService.I18n.T("添加")</MButton>
            }
            @if (IsShowEditButton)
            {
                <MButton Color="info" Class="mx-2 my-1" OnClick=@(()=>EditClick(selectedItem?.ToArray()))>@AppService.I18n.T("修改")</MButton>
            }
            @if (IsShowDeleteButton)
            {
                <MButton Color="error" Loading="DeleteLoading" Class="mx-2 my-1" OnClick=@(()=>DeleteClick(selectedItem?.ToArray()))>@AppService.I18n.T("删除")</MButton>
            }
            @if (OtherToolbarTemplate != null)
            {
                @OtherToolbarTemplate(selectedItem)
            }
            @if (IsShowFilter)
            {
                <div class="ml-auto mr-2 my-1">
                    <TableFilter FilterHeaderString="FilterHeaderString"
                                 ValueChanged="FilterChanged">
                    </TableFilter>
                </div>
            }
        </MRow>

    </MCard>
}

<MCard Class="ma-2" Outlined Rounded=true Elevation=1 @onkeydown="Enter">
     @* FixedHeader=@(!IsMobile)  *@
     @* Height=@(!IsMobile?"100%":null) *@
     @* Style=@((IsMobile ? "overflow-y:hidden !important" : "overflow-y:auto;")) *@
     @* ResizeMode=DataTableResizeMode.Auto *@

     <MDataTable HideDefaultFooter MultiSort
                 ShowSelect=IsShowSelect
                 Height="TableHeight"
                 Dense=@(Dense)
                 @bind-Value="selectedItem"
                 @ref="_table" Stripe
                 OnOptionsUpdate="HandleOnOptionsUpdate"
                 ServerItemsLength="PageItems.Total"
                 Page="SearchModel.Current"
                 ItemsPerPage="SearchModel.Size"
                 OnPageCount="PageChanged"
                 Loading="QueryLoading"
                 ItemKey="r=>r.Id.ToString()"
                 Items="Items"
                 Headers="headers">

         <HeaderColContent Context="header">
             @if (HeaderTemplate != null)
            {
                @HeaderTemplate(header)
            }
            else
            {
                <MLabel Class="text-subtitle">@header.Text</MLabel>
            }
        </HeaderColContent>
        <ItemColContent>
            @if (context.Header.Value == BlazorAppService.DataTableActions)
            {
                <MRow Dense>
                 @if (ItemColOperTemplate != null)
                    {
                        if (IsMenuOperTemplate)
                        {
                            <MMenu OffsetY CloseOnClick=true Context="menu">
                                 <ActivatorContent>
                                     <MButton Small @attributes="@menu.Attrs" Text>
                                     <MIcon>mdi-dots-vertical</MIcon>
                                 </MButton>
                             </ActivatorContent>
                             <ChildContent>
                                 <MSheet>
                                     <MContainer>
                                         <MRow Dense>
                                             @ItemColOperTemplate(context)
                                            </MRow>
                                        </MContainer>
                                    </MSheet>

                                </ChildContent>
                            </MMenu>
                        }
                        else
                        {
                            @ItemColOperTemplate(context)
                        }
                    }
                    @if (IsShowEditButton)
                    {
                        <MTooltip Bottom Context="tip">
                             <ActivatorContent>
                                 <MButton Color="info" Small @attributes="@tip.Attrs"
                                          OnClick="()=>EditClick(context.Item)"
                                              Text>
                                 <MIcon>mdi-pencil</MIcon>
                             </MButton>
                         </ActivatorContent>
                         <ChildContent>
                             <span>@AppService.I18n.T("修改")</span>
                            </ChildContent>
                        </MTooltip>
                    }
                    @if (IsShowDeleteButton)
                    {
                        <MTooltip Bottom Context="tip">
                             <ActivatorContent>
                                 <MButton Color="error" Small @attributes="@tip.Attrs"
                                          OnClick="()=>DeleteClick(context.Item)"
                                              Text>
                                 <MIcon>mdi-delete</MIcon>
                             </MButton>
                         </ActivatorContent>
                         <ChildContent>
                             <span>@AppService.I18n.T("删除")</span>
                            </ChildContent>
                        </MTooltip>
                    }
                    @if (IsShowDetailButton)
                    {
                        <MTooltip Bottom Context="tip">
                             <ActivatorContent>
                                 <MButton Color="info" Small @attributes="@tip.Attrs"
                                          OnClick="()=>DetailClick(context.Item)"
                                              Text>
                                 <MIcon>mdi-information</MIcon>
                             </MButton>
                         </ActivatorContent>
                         <ChildContent>
                             <span>@AppService.I18n.T("详情")</span>
                            </ChildContent>
                        </MTooltip>
                    }
                </MRow>
            }
            else
            {
                if (ItemColWithDTTemplate != null)
                {
                    @ItemColWithDTTemplate(context)
                }
                else
                {
                    if (context?.Value?.GetType() == typeof(DateTime))
                    {
                        <span>
                            @(((DateTime)context.Value).ToDefaultDateTimeFormat(AppService.TimezoneOffset))
                        </span>
                    }
                    else if (ItemColTemplate != null)
                    {
                        @ItemColTemplate(context)
                    }
                    else
                    {
                        @if (context.Header.CellClass?.Contains("text-truncate") == true)
                        {
                            <span title=@context.Value>
                                @context.Value
                            </span>
                        }
                        else
                        {
                            @context.Value
                        }

                    }
                }


            }
        </ItemColContent>
    </MDataTable>

    @if (IsPage)
    {
        <MSheet Class="d-flex flex-row justify-start align-center  py-1 mt-2" Elevation="0">
            <MRow NoGutters Align="AlignTypes.Center" Class="ml-1">

                 <div class="px-1 py-1" style="white-space: nowrap;">
                     @(AppService.I18n.T("共 {0} 条", args: PageItems.Total))
                 </div>
                 <MSelect Class="px-1 py-1" Items=@(PageSizeItems)
                          @bind-Value=@(SearchModel.Size)
                          MenuProps="@(props => { props.Top = true; props.OffsetY = true; })"
                          ItemText=@(u =>AppService.I18n.T("{0} 条/页", args: u.Key))
                          ItemValue=@(u => u.Value)
                          HideDetails="true" Height="30"
                      Dense Style="max-width:150px"
                      Outlined>
             </MSelect>

             <MTextField Dense Style="max-width:150px" HideDetails=@("auto") Class="px-1 py-1"
                             @bind-Value="SearchModel.Current" Label=@AppService.I18n.T("页面跳转")
                             NumberProps=@(prop=> { prop.Min = 1; prop.Step = 1; }) TValue=int Type="number" Outlined />


                 <MPagination Class="ml-auto mr-1 py-1" Length="PageItems.Pages"
                              @bind-Value="SearchModel.Current"
                              TotalVisible="5" Color="primary" Circle></MPagination>

             </MRow>
         </MSheet>

    }

</MCard>

@*Add*@
@if (AddShow)
{
    <PModal @bind-Value="AddShow"
            FormModel="AddModel"
        Persistent AutoScrollToTop
             Width=@(IsMobile?"100%":AddWidth)
             Title=@AppService.I18n.T("添加")
             OnSave="AddOnSave"
             OnCancel="AddOnCancel">
         @if (AddTemplate != null && AddShow)
        {
            @AddTemplate(AddModel)
        }
    </PModal>
}
@if (EditShow)
{
    @*Edit*@
    <PModal @bind-Value="EditShow"
            FormModel="EditModel"
        Persistent AutoScrollToTop
             Width=@(IsMobile?"100%":EditWidth)
             Title=@AppService.I18n.T("修改")
             OnSave="EditOnSaveAsync"
             OnCancel="EditOnCancel">
         @if (EditTemplate != null && EditShow)
        {
            @EditTemplate(EditModel)
        }
    </PModal>
}

@*Detail*@
<PDrawer @bind-Value="DetailShow"
         AutoScrollToTop
          Title=@AppService.I18n.T("详情")
          Width=@(IsMobile?"100%":DetailWidth)>
     <MContainer>
         <MSimpleTable Dense FixedHeader>
            <tbody>
                @if (DetailShow)
                {
                    @foreach (var item in Headers)
                    {
                        if (item.Value != BlazorAppService.DataTableActions)
                        {
                            string value = DetailModelPairs[item.Value];
                            {
                                @if (Detailemplate != null)
                                {
                                    @Detailemplate((item, value))
                                }
                                else
                                {
                                    <tr @key="item.Text">
                                        <td class="text-start table-minwidth">
                                            @item.Text
                                        </td>
                                        <td class="text-start ">
                                            <div style="word-break:break-all; white-space:pre-wrap;">
                                                @value
                                            </div>
                                        </td>
                                    </tr>
                                }

                            }
                        }
                    }
                }

            </tbody>
        </MSimpleTable>
    </MContainer>
</PDrawer>
